package com.hjimi.ski.view.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.hjimi.ski.R;

/**
 * Created By 孙岩
 * on 2022/7/28
 */
public class NoWifiPopup extends BaseDialog implements View.OnFocusChangeListener , View.OnClickListener {
    private TextView tvCancel;
    private TextView tvSubmit;
    private IMode iMode;
    private Activity activity;

    public NoWifiPopup(Activity context) {
        super(context);
        this.activity = context;
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(getOwnerActivity().LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popup_no_wifi,
                null);
        tvCancel = (TextView) view.findViewById(R.id.tvCancel);
        tvSubmit = (TextView) view.findViewById(R.id.tvSubmit);
        tvCancel.setOnFocusChangeListener(this);
        tvSubmit.setOnFocusChangeListener(this);
        tvCancel.setOnClickListener(this);
        tvSubmit.setOnClickListener(this);

        setContentView(view);
        getWindow().setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

//    @Override
//    protected Animation initShowAnimation() {
//        return null;
//    }
//
//    @Override
//    public View getClickToDismissView() {
//        return getPopupWindowView();
//    }
//
//    @Override
//    public View onCreatePopupView() {
//        return createPopupById(R.layout.popup_no_wifi);
//    }
//
//    @Override
//    public View initAnimaView() {
//        return findViewById(R.id.rlStart);
//    }


    public void setModeListener(IMode iMode) {
        this.iMode = iMode;
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (view.getId() == R.id.tvCancel) {
            tvCancel.setBackground(activity.getResources().getDrawable(R.drawable.bg_line_orange));
            tvSubmit.setBackground(null);

            view.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                        switch (i) {
                            case KeyEvent.KEYCODE_ENTER:
                                iMode.getMode(0);
                                dismiss();
                                break;
                            case KeyEvent.KEYCODE_DPAD_CENTER:
                                iMode.getMode(0);
                                dismiss();
                                break;
                        }
                    }
                    return false;
                }
            });
        } else if (view.getId() == R.id.tvSubmit) {
            tvCancel.setBackground(null);
            tvSubmit.setBackground(activity.getResources().getDrawable(R.drawable.bg_line_orange));

            view.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                        switch (i) {
                            case KeyEvent.KEYCODE_ENTER:
                                iMode.getMode(1);
                                dismiss();
                                break;
                            case KeyEvent.KEYCODE_DPAD_CENTER:
                                iMode.getMode(1);
                                dismiss();
                                break;
                        }
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.tvSubmit){
            iMode.getMode(1);
        }
    }

    public interface IMode {
        void getMode(int mode);
    }
}
